|
|
|
|
|
|
|
Remember that the Input # statement looks for those delimiters, so make sure that your Input # statements match the format of the file. |
|
|
|
|
|
|
|
|
Another method of reading information is the Input function. The Input function allows you to specify the number of characters to read from the file, as in the following example: |
|
|
|
|
|
|
|
|
'Reads five Character from file number 1
s = Input(5,#1) |
|
|
|
|
|
|
|
|
Now, compare how each of the methods just discussed would process the same line in a file: |
|
|
|
|
|
|
|
|
'Assume our file has the following line repeated in it:
"This is a test string."
Dim s As String
Line Input #1, s
's contains the entire string including quotes
Input #1, s
's contains the string without quotes
s = Input(5,#1)
's Contains the first 5 characters ("This) |
|
|
|
|
|
|
|
|
Writing to a Sequential Text File |
|
|
|
|
|
|
|
|
One good use of a sequential text file is a log file. For example, I have a scheduler application that runs programs and database updates. I rarely work at the machine on which the scheduler application is running, but I can connect over the network and view the log file to see whether the updates have completed. |
|
|
|
|
| |
|
|
|
|
You can create batch files, FTP scripts, and many other simple file formats on-the-fly by using sequential text files. |
|
|
|
|
|
|
|
|
|
|
Listing 21.2 is a subroutine called LogPrint, which can be added to your program to log error messages. It writes the error message and date to a sequential text file. |
|
|
|
|
|
|
|
|
Listing 21.2 LOGPRINT.ZIPUsing a Sequential Output File to Build an Application Log |
|
|
|
|
|
|
|
|
Sub LogPrint(sMessage As String)
Dim nFile As Integer
nFile = FreeFile
Open App.Path & "\ErrorLog.TXT" for Append Shared as #nFile
Print #nFile,format$(Now,"mm-dd hh:mm:ss") & " " & sMessage
Close #nFile
End Sub |
|
|
|
|
|
|
|
|
The function can be called from an error routine or to inform you of a program event: |
|
|
|
|
|
|
|
|
LogPrint "The database was successfully opened." |
|
|
|
|
|